package jwlee.testreflection;

import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * 利用内省处理JavaBean 类属性、事件
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright:  Copyright (c) 2012</p>
 * <p>Company:  </p>
 * 
 * @author mzj
 * @date 2012-7-11
 * @version 1.0
 */
public class IntrospectorTest {

	/**
	 * @param args
	 * @throws IntrospectionException
	 */
	public static void main(String[] args) throws IntrospectionException {
		BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);

		System.out.println("===========BeanDescriptor============");
		BeanDescriptor beanDesc = beanInfo.getBeanDescriptor();
		Class cls = beanDesc.getBeanClass();
		System.out.println(cls.getName());

		System.out.println("===========MethodDescriptor==========");
		MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();
		for (int i = 0; i < methodDescs.length; i++) {
			Method method = methodDescs[i].getMethod();
			System.out.println(method.getName() + "()");
		}

		System.out.println("===========PropertyDescriptor========");
		PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < propDescs.length; i++) {
			sb.append(propDescs[i].getName()).append(": ");
			Method methodR = propDescs[i].getReadMethod();
			Method methodW = propDescs[i].getWriteMethod();
			if (methodR != null) {
				sb.append(methodR.getName()).append("();");
			}
			if (methodW != null) {
				sb.append(methodW.getName()).append("();");
			}
			sb.append("\n");
		}
		System.out.println(sb);
	}

}

class Person {
	private String name;
	private int age;
	
	Person(String name){
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}
}